package com.gzx.plugin.tjzy.feign.debtor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.feign.debtor.mapper.GzxDebtorInfoFeignMapper;
import com.gzx.plugin.tjzy.feign.debtor.service.GzxDebtorInfoFeignService;
import com.gzx.tjzy.feign.req.debtor.GzxDebtorInfoReqDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/2/13 10:03
 */
@DS("tjzy")
@Service
@RequiredArgsConstructor
public class GzxDebtorInfoFeignServiceImpl implements GzxDebtorInfoFeignService {

    private final GzxDebtorInfoFeignMapper debtorInfoFeignMapper;

    /**
     * 批量新增欠款人信息
     *
     * @param debtorInfoReqDtoList
     */
    @Override
    public void insertBatch(List<GzxDebtorInfoReqDto> debtorInfoReqDtoList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxDebtorInfoReqDto>> batchList = Lists.partition(debtorInfoReqDtoList, 500);
        for (List<GzxDebtorInfoReqDto> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s', '%s', '%s','%s','%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                            escapeSql(s.getProjectCode()),        // project_code
                            escapeSql(s.getDebtorName()),         // debtor_name
                            escapeSql(String.valueOf(s.getDebtorSex())),                     // debtor_sex
                            escapeSql(s.getDebtorPhone()),       // debtor_phone
                            escapeSql(s.getDebtorCardId()),       // debtor_card_id
                            escapeSql(s.getRealDebtorCardId()),   // real_debtor_card_id
                            escapeSql(s.getDebtorBirth()),        // debtor_birth
                            escapeSql(String.valueOf(s.getDebtorAge())),                     // debtor_age
                            escapeSql(s.getDebtorProvince()),    // debtor_province
                            escapeSql(s.getDebtorCity()),         // debtor_city
                            escapeSql(s.getDebtorAddress()),     // debtor_address
                            escapeSql(s.getDebtorNowAddress()),   // debtor_now_address
                            escapeSql(String.valueOf(s.getDebtorAccountBalance())),         // debtor_account_balance
                            escapeSql("1543842934270394368"),    // tenant_id
                            escapeSql(currentTime)                   // create_time
                    ))
                    .collect(Collectors.joining(", "));
            debtorInfoFeignMapper.insertBatch(batchSql.replace("'null'", "null"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }
}
